//
//  XZ_BaseTableViewController.m
//  Project_Demo
//
//  Created by caoxiaozhu on 2018/6/29.
//  Copyright © 2018年 caoxiaozhu. All rights reserved.
//

#import "XZ_BaseTableViewController.h"

@interface XZ_BaseTableViewController ()

@property (nonatomic, copy) NSArray *titles;

@property (nonatomic, strong) UIViewController *vc;
@end

@implementation XZ_BaseTableViewController

- (void)setupDataArr:(NSArray *)dataArr{
//    if (_dataArr == nil) {
//        _dataArr = [NSArray array];
//    }
//    return _dataArr;
    
    NSMutableArray *tempTitleArr = [NSMutableArray array];
    NSMutableArray *tempNamesArr = [NSMutableArray array];
    
    for (NSArray *arr in dataArr) {
        [tempTitleArr addObject:arr[0]];
        [tempNamesArr addObject:arr[1]];
    }
    self.titles = tempTitleArr;
    self.classNames = tempNamesArr;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//#warning message: 自动行高 
//    self.tableView.rowHeight = UITableViewAutomaticDimension;
//    self.tableView.estimatedRowHeight = _estimateRowHeight;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (_sectionNum) {
        return _sectionNum;
    }else{
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:labelCellid];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:labelCellid];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%ld-%@",indexPath.row+1,self.titles[indexPath.row]];
    cell.detailTextLabel.text = self.classNames[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    
    NSString *className = self.classNames[indexPath.row];
//    NSLog(@"className = %@", className);
    
    if ([className isEqualToString:@"SwiftBasicTableView"]) {
        SwiftBasicTableView *swift = [[SwiftBasicTableView alloc]init];
        _vc = swift;
    }else{
        _vc = [[NSClassFromString(className) alloc] init];
    }
    
    _vc.title = self.titles[indexPath.row];
    [self.navigationController pushViewController:_vc animated:YES];
    
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 60;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
